<?php


// no direct access
defined('_JEXEC') or die('Restricted access');


class modChatterHelper
{
	static $smilies = array(
		':)' => '<img height="15" src="images/smilies/smile.gif" width="15"/>',
		':-)' => '<img height="15" src="images/smilies/smile.gif" width="15"/>',
		':smile:' => '<img height="15" src="images/smilies/smile.gif" width="15"/>',

		':D' => '<img height="15" src="images/smilies/biggrin.gif" width="15"/>',
		':-D' => '<img height="15" src="images/smilies/biggrin.gif" width="15"/>',
		':grin:' => '<img height="15" src="images/smilies/biggrin.gif" width="15"/>',

		';)' => '<img height="15" src="images/smilies/wink.gif" width="15"/>',
		';-)' => '<img height="15" src="images/smilies/wink.gif" width="15"/>',
		':wink:' => '<img height="15" src="images/smilies/wink.gif" width="15"/>',

		':P' => '<img height="15" src="images/smilies/tongue.gif" width="15"/>',
		':-P' => '<img height="15" src="images/smilies/tongue.gif" width="15"/>',
		':tongue:' => '<img height="15" src="images/smilies/tongue.gif" width="15"/>',

		':(' => '<img height="15" src="images/smilies/sad.gif" width="15"/>',
		':-(' => '<img height="15" src="images/smilies/sad.gif" width="15"/>',
		':sad:' => '<img height="15" src="images/smilies/sad.gif" width="15"/>',
	);
	
	static $formatting = array(
		'|/([^/]+)/|'	=> '<i>$1</i>',
		'/\*([^*]+)\*/'	=> '<b>$1</b>',
		'/_([^_]+)_/'	=> '<u>$1</u>',
	);

    static function getLatestChatter($since = 0)
    {
		jimport('joomla.filter.output');
		$db = JFactory::getDBO();
		$query = 'SELECT c.nick, c.message'
			. "\n  FROM #__chatter c"
			. ($since > 0 ? "\n  WHERE c.posted > " . (int)$since : '')
			. "\n  ORDER BY c.posted DESC"
			;
		// If you increase the number of lines you better add more colors.
		$db->setQuery($query, 0, 20);

		$colorIndex = 0;
		$colorsByNick = array();
		$chatter = new stdClass();
		$chatter->lines = array();
		foreach ($db->loadObjectList() as $line)
		{
			// Prevent XSS attacks.
			JFilterOutput::objectHTMLSafe($line);

			// Assign a unique color for the nick.
			if (!isset($colorsByNick[$line->nick]))
				$colorsByNick[$line->nick] = 'color' . ($colorIndex++);
			$line->color = $colorsByNick[$line->nick];

			$chatter->lines[] = $line;
		}
		
		$query = 'SELECT MAX(c.id) last_line_posted'
			. "\n  FROM #__chatter c"
			;
		$db->setQuery($query);
		$chatter->lastLinePosted = $db->loadResult();
		
		// Clean up.
		$query = 'DELETE FROM #__chatter'
			. "\n  WHERE id < (SELECT id FROM #__chatter LIMIT 20, 1)"
			;
		$db->setQuery($query);
		$db->query();

		return $chatter;
    }

    static function formatMessage($message)
    {
		$message = preg_replace(array_keys(modChatterHelper::$formatting), array_values(modChatterHelper::$formatting), $message);
		$message = str_replace(array_keys(modChatterHelper::$smilies), array_values(modChatterHelper::$smilies), $message);
		return $message;
    }


	function postLine($nick, $message, &$error)
	{
		$session = JFactory::getSession();
		if ('' == $nick)
		{
			$error = JText::_('Please provide a Nick.');
			return false;
		}
		$session->set('chatter.nick', $nick);
		if ('' == $message)
		{
			$error = JText::_('Please provide a Message.');
			return false;
		}
	
		$db = JFactory::getDBO();
		$query = 'INSERT INTO #__chatter (nick, message)'
			. "\n  VALUES('" . $db->getEscaped($nick) . "', '" . $db->getEscaped($message) . "')"
			;
		$error = null;
		$db->setQuery($query);
		if ($db->query() == false)
		{
			$error = $db->getErrorMsg();
			return false;
		};
		return true;
	}
}


?>
